home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-03-04 | 7.4 KB | 313 lines | [TEXT/ToyS] |
- -- Preferences
- property kasPrefName : "Width / Height Removal"
-
- property kasTypesToDo : {"TEXT"} -- Only run on these types
- property kasExtToDo : "" -- Use non-empty match string (e.g. "*.html") to limit to that extension
-
- -- Globals
- global gasInfoWind -- Info window
- global gasInfoPos -- Position of info window
- global gasFoldersToDo -- The folders left to process
- global gasConverted -- Number gone!
- global gasChecked -- Number checked!
- global gasImages -- Number images checked!
- global gasPrefix -- Adjusted kasPrefix (munge the % parms)
- global gasErrorFiles -- List of files with errors
- global gasErrorText -- Text list of errors
-
-
- on open fsObjs
- -- Load prefs, show window
- pfLoad()
-
- set gasConverted to 0
- set gasChecked to 0
- set gasImages to 0
- set gasErrorFiles to {}
- set gasErrorText to ""
-
- set gasInfoWind to display info titled kasPrefName ¬
- located at gasInfoPos ¬
- message "Scanning…"
-
- -- Do files
- set gasFoldersToDo to {}
-
- repeat with fsObj in fsObjs
- set myInfo to (basic info for fsObj)
-
- if (system type of myInfo is "fold") then
- set gasFoldersToDo to gasFoldersToDo & {fsObj}
- else if (system type of myInfo is in kasTypesToDo) then
- if (kasExtToDo is "") or ((collect lines of (catalog name of myInfo) that match kasExtToDo) is not "") then
- DoOne(fsObj)
- end if
- end if
- end repeat
-
- -- Do folders
- repeat while gasFoldersToDo is not {}
- -- Pop one off the end
- set n to the number of items of gasFoldersToDo
- set fsObj to item n of gasFoldersToDo
-
- if (n > 1) then
- set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
- else
- set gasFoldersToDo to {}
- end if
-
- ShowToGo(n)
-
- -- Process it
- GoDeep(fsObj)
- end repeat
-
- display info gasInfoWind message "DONE!"
-
- pause for 2 with seconds timing -- Let screen wait...
-
- set gasInfoPos to screen location of ¬
- (display info gasInfoWind with disposal)
-
- pfSave() -- Save window location
-
- if (gasErrorText is not "") then
- clip gasErrorText
- tell application "Note Pad" to activate
- input state {keys down:"N", command key down:true} -- New note
- input state {keys down:"V", command key down:true} -- Paste errors
- activate
- end if
-
- if (gasErrorFiles is not {}) then
- display dialog "Open files with errors?" default button 2
- tell application "Finder" to open gasErrorFiles
- end if
- end open
-
-
- on DoOne(fsObj)
- set aFileInfo to (alias info from fsObj)
- ShowChecked(original name of aFileInfo)
- ConvertFile(fsObj)
- end DoOne
-
-
- on ConvertFile(fsObj)
- try
- set fileData to ¬
- read data from the data fork of fsObj
- on error errStr
- ShowError(fsObj, errStr)
- return
- end try
-
- set fname to catalog name of (basic info for fsObj)
- set html to parse ML fileData
- set sTags to collect items of html that match "%*src=*"
- set vTags to collect items of html that match "%*virtual=*"
- set fTags to collect items of html that match "%*file=*"
- set uTags to collect items of html that match "%*value=*"
-
- if ShowSrcCnt(number of items of (sTags & vTags & fTags & uTags)) then
- set changed to false
-
- -- Check tags!
- repeat with imgTag in imgTags
- set myParms to item 1 of (parse tag (item imgTag of html))
- set oldW to 0
- set oldH to 0
- set hTags to (collect items of myParms that match "%HEIGHT=*")
- set wTags to (collect items of myParms that match "%WIDTH=*")
-
- -- Got height or widht parms?
- if ShowTagCnt(number of items of hTags, number of items of wTags) then
- -- Get oldW, oldH!
- if (hTags is not {}) then set oldH to parse tag val (item (item 1 of hTags) of myParms)
- if (wTags is not {}) then set oldW to parse tag val (item (item 1 of wTags) of myParms)
- -- Remove H/W tags from the parsed tag
- set myParms to (edit list myParms with edits hTags with removal of items)
- set myParms to (edit list myParms with edits wTags with removal of items)
- end if
-
- -- Find our "SRC=" file…
- set sTag to (collect items of myParms that match "%SRC=*")
- if (sTag is {}) then
- ShowError(fsObj, "No SRC tag in " & fname)
- else
- set sTag to item (item 1 of sTag) of myParms
-
- try
- set picFile to (resolve SRC tag sTag relative to file fsObj)
- on error
- ShowError(fsObj, "Can't find: " & sTag)
- set picFile to 0
- end try
-
- if (picFile is not 0) then
- ShowImageFile(picFile as string)
- -- Update the HTML
- set box to picture bounds of (the picture info for (the image from picFile))
- set w to (item 3 of box) - (item 1 of box)
- set h to (item 4 of box) - (item 2 of box)
- if (w is not oldW or h is not oldH) then
- set changed to true
- set w to "WIDTH=" & w
- set h to "HEIGHT=" & h
- set myParms to myParms & {w, h}
- set item imgTag of html to (compile tag myParms)
- end if
- end if
- end if
- end repeat
-
- -- Rename to lowercase?
- if (changed) then
- -- Write data
- write data to the data fork of fsObj from buffer (compile ML html)
- ShowConverted(fname)
- end if
- end if
- end ConvertFile
-
-
- on ShowImgCnt(cnt)
- display info gasInfoWind ¬
- message ("Images: " & cnt) ¬
- at line 10
-
- return cnt > 0
- end ShowImgCnt
-
-
- on ShowTagCnt(cntW, cntH)
- set n to cntW + cntH
- display info gasInfoWind ¬
- message ("Tags: " & n) ¬
- at line 11
-
- return n > 0
- end ShowTagCnt
-
-
- on ShowError(fsObj, errStr)
- set gasErrorFiles to gasErrorFiles & fsObj
- set gasErrorText to gasErrorText & (fsObj as string) & " • " & errStr & return
-
- display info gasInfoWind ¬
- message ("Error: " & errStr) ¬
- at line 20 ¬
- using color (25 * 1024)
- end ShowError
-
-
- on ShowConverted(fname)
- set gasConverted to gasConverted + 1
-
- display info gasInfoWind ¬
- message ("Converted: " & gasConverted) ¬
- at line 8 ¬
- using color (15 * 1024)
-
- display info gasInfoWind ¬
- message ("Last: " & fname) ¬
- at line 9 ¬
- using color (16 * 1024)
- end ShowConverted
-
-
- on ShowChecked(fname)
- set gasChecked to gasChecked + 1
-
- display info gasInfoWind ¬
- message ("File: " & fname) ¬
- at line 6
-
- display info gasInfoWind ¬
- message ("Checked: " & gasChecked) ¬
- at line 7 ¬
- using color 15
- end ShowChecked
-
-
- on ShowImageFile(fname)
- set gasImages to gasImages + 1
-
- display info gasInfoWind ¬
- message ("Image: " & fname) ¬
- at line 15
-
- display info gasInfoWind ¬
- message ("Images: " & gasImages) ¬
- at line 16
- end ShowImageFile
-
-
- on ShowAction(msg)
- display info gasInfoWind ¬
- message msg ¬
- at line 2 ¬
- using color (15 * 1024 + 15)
- end ShowAction
-
-
- on ShowToGo(n)
- display info gasInfoWind ¬
- message ("Folders to go: " & n) ¬
- at line 14 ¬
- using color (15 * 32)
- end ShowToGo
-
-
- on GoDeep(foldObj)
- display info gasInfoWind ¬
- message "Path: " & (foldObj as string)
-
- set daddy to foldObj as string
-
- -- Do kinds that match
- ShowAction("Scanning files…")
-
- if (kasExtToDo is "") then
- set myItems to the entries in foldObj ¬
- whose types are in kasTypesToDo
- else
- set myItems to the entries in foldObj ¬
- whose types are in kasTypesToDo ¬
- whose names match kasExtToDo
- end if
-
- repeat with myItem in myItems
- DoOne((daddy & myItem) as alias)
- end repeat
-
- -- Do folders
- ShowAction("Scanning subfolders")
-
- set myItems to the entries in foldObj ¬
- whose kinds are a folder
-
- repeat with myItem in myItems
- set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
- end repeat
-
- -- Done
- ShowAction("…")
- end GoDeep
-
-
- on pfLoad()
- try
- set ourPrefs to (load preference named kasPrefName)
- set gasInfoPos to item 1 of ourPrefs
- on error
- set gasInfoPos to {-1, -1}
- end try
- end pfLoad
-
-
- on pfSave()
- save preference {gasInfoPos} named kasPrefName
- end pfSave
-